package com.koron.css2.projectCenter.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.projectCenter.bean.query.PjInstallQueryBean;
import com.koron.css2.projectCenter.mapper.PjInstallMapper;
import com.koron.css2.projectCenter.vo.PjInstallListVO;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;
import java.util.Map;

/**
 * 用水安装列表
 * @author 许海宜
 * 2020年3月5日
 */
public class PjInstallList implements ServerInterface {

	@Override
	@SuppressWarnings("rawtypes")
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		try {
			//将入参转换为QueryBean
			PjInstallQueryBean queryBean = JsonUtils.objectToPojo(req.getData(), PjInstallQueryBean.class);
			if(StringUtils.isBlank(queryBean.getBeginDate())||StringUtils.isBlank(queryBean.getEndDate())) {
				return MessageBean.create(Constant.NOT_NULL, "单据日期不能为空", null);
			}
			//获取Mapper实例
			PjInstallMapper mapper = factory.getMapper(PjInstallMapper.class);			
			//设置分页参数
			PageHelper.startPage(queryBean.getPage(), queryBean.getPageCount());
			//执行SQL
			List<PjInstallListVO> list = mapper.list(queryBean);		
			//获取紧急程度数据字典
			Map<String, String> egd = Tools.mapDicByCode(factory, "EGD");
			Map<String, String> ppt = Tools.mapDicByCode(factory, "PPT");
			Map<String, String> pis = Tools.mapDicByCode(factory, "PIS");
			//转换状态字段
			for(PjInstallListVO bean:list) {
				//转换数据字典
				bean.setUrgentFlagName(egd.get(bean.getUrgentFlag()));
				bean.setPjType(ppt.get(bean.getPjType()));
				bean.setSourceFlag(pis.get(bean.getSourceFlag()));
				//转换进度
				bean.setProcessState(Tools.transProcessStatus(bean.getProcessState()));
			}
			//输出分页结果
			MessageBean<PageInfo> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功", PageInfo.class);
			info.setData(new PageInfo<>(list));
			return info;
		} catch (Exception e) {
			logger.error("用水咨询列表查询失败"+e.getMessage(), e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "查询失败", null);
		}
	}


}
